//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef _ARM_LH7A400_SERIAL_H_
#define _ARM_LH7A400_SERIAL_H_

#define SERIAL_ARM_LH7A400_SERIAL_OUT_BUFSIZE  1024
#define SERIAL_ARM_LH7A400_SERIAL_IN_BUFSIZE   2048

#define SERIALPKG_IO_SERIAL_ARM_LH7A400_SERIAL0
#define SERIALNUM_IO_SERIAL_ARM_LH7A400_SERIAL0_BAUD 115200

//---------------------------------------------------------------------------
// UARTs

#define AAECNUM_HAL_INTERRUPT_UART1INTR    12
#define AAECNUM_HAL_INTERRUPT_UART2INTR    13
#define AAECNUM_HAL_INTERRUPT_UART3INTR    16

#define AAEC_UART1                0x80000600
#define AAEC_UART2                0x80000700
#define AAEC_UART3                0x80000800

#define AAEC_UART2_UMS2EOI        0x80000714 // modem end of interrupt
#define AAEC_UART2_UMS3EOI        0x80000814 // modem end of interrupt

#define AAEC_UART_DATA         0x0000 // Data/FIFO register
#define AAEC_UART_LCR          0x0004 // Control register
#define AAEC_UART_LCR_BRK      0x0001 // Send break
#define AAEC_UART_LCR_PEN      0x0002 // Enable parity
#define AAEC_UART_LCR_EP       0x0004 // Odd/Even parity
#define AAEC_UART_LCR_S2       0x0008 // One/Two stop bits
#define AAEC_UART_LCR_FIFO     0x0010 // Enable FIFO
#define AAEC_UART_LCR_WL5      0x0000 // Word length - 5 bits
#define AAEC_UART_LCR_WL6      0x0020 // Word length - 6 bits
#define AAEC_UART_LCR_WL7      0x0040 // Word length - 7 bits
#define AAEC_UART_LCR_WL8      0x0060 // Word length - 8 bits
#define AAEC_UART_BAUD         0x0008 // Baud rate
#define AAEC_UART_CTRL         0x000C // Control register
#define AAEC_UART_CTRL_ENAB    0x0001 // Enable uart
#define AAEC_UART_CTRL_SIR     0x0002 // Enable SIR IrDA
#define AAEC_UART_CTRL_SIRLP   0x0004 // Enable low power IrDA
#define AAEC_UART_CTRL_RXP     0x0008 // Receive pin polarity
#define AAEC_UART_CTRL_TXP     0x0010 // Transmit pin polarity
#define AAEC_UART_CTRL_MXP     0x0020 // Modem pin polarity
#define AAEC_UART_CTRL_LOOP    0x0040 // Loopback mode
#define AAEC_UART_CTRL_SIRBD   0x0080 // blanking disable
#define AAEC_UART_STATUS       0x0010 // Status
#define AAEC_UART_STATUS_CTS   0x0001 // Clear-to-send status
#define AAEC_UART_STATUS_DSR   0x0002 // Data-set-ready status
#define AAEC_UART_STATUS_DCD   0x0004 // Data-carrier-detect status
#define AAEC_UART_STATUS_TxBSY 0x0008 // Transmitter busy
#define AAEC_UART_STATUS_RxFE  0x0010 // Receive FIFO empty
#define AAEC_UART_STATUS_TxFF  0x0020 // Transmit FIFO full
#define AAEC_UART_STATUS_RxFF  0x0040 // Receive FIFO full
#define AAEC_UART_STATUS_TxFE  0x0080 // Transmit FIFO empty
#define AAEC_UART_INT          0x0014 // Interrupt status
#define AAEC_UART_INTM         0x0018 // Interrupt mask register
#define AAEC_UART_INTRES       0x001c // Interrupt result (masked interrupt status)
#define AAEC_UART_INT_RIS      0x0001 // Rx interrupt
#define AAEC_UART_INT_TIS      0x0002 // Tx interrupt
#define AAEC_UART_INT_MIS      0x0004 // Modem status interrupt
#define AAEC_UART_INT_RTIS     0x0008 // Rx timeout interrupt
#define AAEC_UART_INT_MISEOI   0x0100 // Clear Modem interrupt (bit4 to bit32)


// ---------------------------------------
//

static unsigned char select_word_length[] = {
    AAEC_UART_LCR_WL5,    // 5 bits / word (char)
    AAEC_UART_LCR_WL6,
    AAEC_UART_LCR_WL7,
    AAEC_UART_LCR_WL8
};

static unsigned char select_stop_bits[] = {
    0,
    0,    // 1 stop bit
    AAEC_UART_LCR_S2,  // 1.5 stop bit (I think)
    AAEC_UART_LCR_S2     // 2 stop bits
};

static unsigned char select_parity[] = {
    0,     // No parity
    AAEC_UART_LCR_PEN|AAEC_UART_LCR_EP,     // Even parity
    AAEC_UART_LCR_PEN,     // Odd parity
    0,     // Mark parity
    0,     // Space parity
};

// Baud rate values, based on 7.3728MHz clock
// #define BAUD_RATE(_n_) ((7372800/((_n_)*16))-1)

static unsigned short select_baud[] = {
    0,  // Unused
    0x23ff,  // 50
    0x17ff,  // 75
    0x105c,  // 110
    0x0d60,  // 134.5
    0x0bff,  // 150
    0x08ff,  // 200
    0x05ff,  // 300
    0x02ff,  // 600
    0x017f,  // 1200
    0x00ff,  // 1800
    0x00bf,  // 2400
    0x007f,  // 3600
    0x005f,  // 4800
    0x003f,  // 7200
    0x002f,  // 9600
    0x001f,  // 14400
    0x0017,  // 19200
    0x000b,  // 38400
    0x0007,  // 57600
    0x0003,  // 115200
    0x0001,  // 230400
};

#endif // _ARM_LH7A400_SERIAL_H_

